package cn.net.shuxing.yifu.common.service;


import cn.net.shuxing.yifu.common.bean.CoinType;
import cn.net.shuxing.yifu.common.bean.LevelGrant;
import cn.net.shuxing.yifu.common.bean.ShareholderLevel;
import cn.net.shuxing.yifu.common.mapper.LevelGrantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class LevelGrantService extends BaseService<LevelGrant, LevelGrantMapper> {

    @Autowired
    private ShareholderLevelService shareholderLevelService;

    @Autowired
    private CoinTypeService coinTypeService;

    public List<LevelGrant> getsByLevel(Integer level) {
        LevelGrant levelGrant = new LevelGrant();
        levelGrant.clearCreateTimeAndUpdateTime();
        levelGrant.setLevel(level);
        return gets(levelGrant);
    }

    public LevelGrant getsByLevelAndCoinTypeId(Integer level, Integer coinTypeId) {
        LevelGrant levelGrant = new LevelGrant();
        levelGrant.clearCreateTimeAndUpdateTime();
        levelGrant.setLevel(level);
        levelGrant.setCoinTypeId(coinTypeId);
        return get(levelGrant);
    }

    public List<LevelGrant> getsByCoinTypeId(Integer coinTypeId) {
        LevelGrant levelGrant = new LevelGrant();
        levelGrant.clearCreateTimeAndUpdateTime();
        levelGrant.setCoinTypeId(coinTypeId);
        List<LevelGrant> gets = gets(levelGrant);
        ArrayList<LevelGrant> levelGrants = new ArrayList<>();
        for (LevelGrant get : gets) {
            Integer coin = get.getCoinTypeId();
            CoinType coinType = coinTypeService.getByPrimaryKey(coin);
            String name = coinType.getName();
            get.setCoinTypeName(name);
            levelGrants.add(get);
        }
        return levelGrants;
    }

    @Override
    public boolean delete(Object primaryKey) {
        Integer level = getByPrimaryKey(primaryKey).getLevel();
        List<ShareholderLevel> shareholderLevels = shareholderLevelService.searchByLevel(level);
        if (shareholderLevels == null || shareholderLevels.isEmpty()) {
            super.delete(primaryKey);
            return true;
        }
        return false;
    }

    public LevelGrant insert(String name, Integer level, Integer coinTypeId, BigDecimal aliveCoinCount, BigDecimal sleepCoinCount) {
        LevelGrant levelGrant = new LevelGrant();
        levelGrant.setName(name);
        levelGrant.setLevel(level);
        levelGrant.setCoinTypeId(coinTypeId);
        levelGrant.setAliveCoinCount(aliveCoinCount);
        levelGrant.setSleepCoinCount(sleepCoinCount);
        insert(levelGrant);
        return getByPrimaryKey(levelGrant.getId());
    }

    public boolean checkLevelAndName(Integer coinTypeId, Integer level, String name) {
        List<LevelGrant> levelGrants = getsByCoinTypeId(coinTypeId);
        boolean check = false;
        for (LevelGrant levelGrant : levelGrants) {
            Integer level1 = levelGrant.getLevel();
            String name1 = levelGrant.getName();
            if (level == level) {
                check = false;
            }
            if (name1 == name) {
                check = false;
            }
        }

        return check;
    }
}
